package com.hlbspringcloud.contentcenter.service.content;

import com.hlbspringcloud.contentcenter.dao.content.ShareMapper;
import com.hlbspringcloud.contentcenter.domain.dto.content.ShareAuditDTO;
import com.hlbspringcloud.contentcenter.domain.dto.content.ShareDTO;
import com.hlbspringcloud.contentcenter.domain.dto.uer.UserDTO;
import com.hlbspringcloud.contentcenter.domain.entity.content.Share;
import com.hlbspringcloud.contentcenter.feignclient.UserCenterFeignClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

/**
 * @Description: 分享service
 * @PROJECT_NAME: spring-cloud-alibaba
 * @CLASS_NAME:
 * @Author: huanglibin
 * @Date: 2020-05-24 2:58
 * @Version: 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ShareService {
    private final ShareMapper shareMapper;

//    private final RestTemplate restTemplate;

//    private final DiscoveryClient discoveryClient;

    private final UserCenterFeignClient userCenterFeignClient;
    public ShareDTO findById(Integer id){
        //获取分享详情
        Share share = this.shareMapper.selectByPrimaryKey(id);
        //发布人id
        Integer userId = share.getUserId();

        //强调
        //了解stream ---> JDK8
        //lambda表达式
        //functional ---> 函数式编程
        //获取用户中心所有实例信息
//        List<ServiceInstance> instances = discoveryClient.getInstances("user-center");
//        String targetUrl = instances.stream()
//                //数据变换
//                .map(instance -> instance.getUri().toString()+"/users/{id}")
//                .findFirst()
//                .orElseThrow(() -> new IllegalArgumentException("当前没有实例"));

        //所有用户中心实例的请求地址
//        List<String> list = instances.stream()
//                .map(instance -> instance.getUri().toString() + "/users/{id}")
//                .collect(Collectors.toList());
//        int idx = ThreadLocalRandom.current().nextInt(list.size());
//
//        log.info("请求的目标地址:{}",list.get(idx));
//        //用HTTP的方法去请求并,并且返回一个对象
        UserDTO userDto = userCenterFeignClient.findById(userId);
        ShareDTO shareDTO = new ShareDTO();
        //消息装配
        BeanUtils.copyProperties(share,shareDTO);
        shareDTO.setWxNickname(userDto.getWxNickname());
        return shareDTO;
    }

    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        //用HTTP的方法去请求并,并且返回一个对象
        ResponseEntity<UserDTO> forEntity = restTemplate.getForEntity(
                "http://localhost:8080/users/{id}",
                UserDTO.class, 2
        );
        System.out.println(forEntity.getBody());
        System.out.println(forEntity.getStatusCode());
        //
    }

    public Share auditById(Integer id, ShareAuditDTO auditDTO) {
        //1.查询Share是否存在,不存在或者当前的audit_status != NOT_YES,那么就抛异常
        Share share = this.shareMapper.selectByPrimaryKey(id);
        if (share==null){
            throw new IllegalArgumentException("参数非法,该分项不存在！");
        }
        if (!Objects.equals("NOT_YES",share.getAuditStatus())){
            throw new IllegalArgumentException("参数非法,该分项以审核通过或者审核不通过！");
        }
        //2.审核资源,将状态设为PASS/REJECT
        share.setAuditStatus(auditDTO.getAuditStatusEnum().toString());
        this.shareMapper.updateByPrimaryKey(share);

        //3.如果是PASS,那么为发布人添加积分
        //异步执行
        userCenterFeignClient.addBonus(id,500);
        return null;
    }
}
